﻿/*
 *
 * Modelcrud.cs
 * For model_name.php in CI with method for crud ( create, read, update, delete )
 * 
 * Author @semarketir
 * 
 * CICI is simple tool for generate Controller, View, and View in CodeIgniter ( or CI ) Framework
 * CICI can used to setup configurations in CI ( Database, Autoload, Config ) <- Next Version
 * CICI just can to used for default directory of CodeIgniter ( or CI ) Framework
 * 
 * CICI v.0.0.4 Beta
 * 
 * Support with CI 2.X ( or heigh )
 * 
 * License Under GPL
 * 
 * 
 * Salam, 
 * semar ketir
 * 
*/

using System;
using System.Collections.Generic;
using System.Text;

namespace cici
{
    class Modelcrud : Model
    {
        public Modelcrud()
        {

        }

        public Boolean createModelcrud(string className)
        {
            return this.createModel(className);
        }

        protected override string templateModel(string file_location, string className)
        {
            string tempClassName = className.Substring(0, 1);
            string realClassName = tempClassName.ToUpper() + className.Remove(0, 1);

            string template =
                "<?php\n\n" +
                "class " + realClassName + " extends CI_Model {\n\n" +
                "\t/**\n" +
                "\t * Model " + realClassName + ".\n" +
                "\t */\n\n" +
                "\tprivate $tableName = '" + className + "';\n\n" +
                "\tprivate $idTable = 'id" + className + "';\n\n" +
                "\tpublic function __construct()\n" +
                "\t{\n" +
                "\t\tparent::__construct();\n" +
                "\t}\n\n" +
                "\t//Get data count of table\n"+
                "\tpublic function countAll()\n" +
                "\t{\n" +
                "\t\treturn $this->db->count_all($this->tableName);\n" +
                "\t}\n\n" +
                "\t//Get all data\n" +
                "\tpublic function getAll()\n" +
                "\t{\n" +
                "\t\t//set your field for asc data in here\n" +
                "\t\t$this->db->order_by($this->idTable,'asc');\n" +
                "\t\treturn $this->db->get($this->tableName)->result();\n" +
                "\t}\n\n" +
                "\t//Get data with page\n" +
                "\tpublic function getByPage($limit = 6, $offset = 0)\n" +
                "\t{\n" +
                "\t\t//set your field for asc data in here\n" +
                "\t\t$this->db->order_by($this->idTable,'asc');\n" +
                "\t\treturn $this->db->get($this->tableName, $limit, $offset)->result();\n" +
                "\t}\n\n" +
                "\t//Get data by id\n" +
                "\tpublic function getById($id)\n" +
                "\t{\n" +
                "\t\t//set your field for get your data\n" +
                "\t\t$this->db->where($this->idTable, $id);\n" +
                "\t\treturn $this->db->get($this->tableName)->result();\n" +
                "\t}\n\n" +
                "\t//Insert new data\n" +
                "\tpublic function insert($data)\n" +
                "\t{\n" +
                "\t\treturn $this->db->insert($this->tableName, $data);\n" +
                "\t}\n\n" +
                "\t//Update data\n" +
                "\tpublic function update($id, $data)\n" +
                "\t{\n" +
                "\t\t//set your field for update your data\n" +
                "\t\t$this->db->where($this->idTable, $id);\n" +
                "\t\treturn $this->db->update($this->tableName, $data);\n" +
                "\t}\n\n" +
                "\t//Delete data\n" +
                "\tpublic function delete($id)\n" +
                "\t{\n" +
                "\t\t//set your field for delete your data\n" +
                "\t\t$this->db->where($this->idTable, $id);\n" +
                "\t\treturn $this->db->delete($this->tableName);\n" +
                "\t}\n\n" +
                "\tpublic function __destruct()\n" +
                "\t{\n" +
                "\t\t\n" +
                "\t}\n" +
                "}\n\n" +
                "/* End of file " + className + ".php */\n" +
                "/* Location: " + file_location + " */\n" +
                "/* Generated by CICI */";

            return template;
        }
    }
}
